package cn.edu.sjtu.literature.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.sjtu.literature.web.util.Constants;
import cn.edu.sjtu.literature.web.view.View;

public class LoggedInControllerWrapper extends AbstractController {

	private Controller target;
	
	public LoggedInControllerWrapper(Controller target) {
		this.target = target;
	}
	
	@Override
	public View doAction(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		if (req.getSession().getAttribute(Constants.SESSION_USER_KEY) == null) {
			// not logged in
			log.debug("not logged in and return to log in page");
			return getRedirectView(req, resp, "loginForm");
		}
		return target.doAction(req, resp);
	}

}
